package com.ayuku.commons.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.Util;

public abstract class BasicFormMaster extends Composite {

	private ToolItem btnEdit;
	private ToolItem btnDelete;
	private ToolItem btnCancel;
	private Composite grpData;
	private ToolItem btnSave;
	private String formKey;
	private int formMode = AYConst.MODE_VIEW;
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public BasicFormMaster(Composite parent, int style, int mode) {
		super(parent, style);
		this.formMode = mode;
		setLayout(new GridLayout(1, false));

		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1,
				1));

		btnSave = new ToolItem(toolBar, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent evt) {
				btnSaveWidgetSelected(evt);
			}
		});
		btnSave.setImage(SWTResourceManager.getImage(BasicFormMaster.class,
				"/images/form/database_add.png"));
		btnSave.setText("Save");

		btnEdit = new ToolItem(toolBar, SWT.NONE);
		btnEdit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent evt) {
				btnEditWidgetSelected(evt);
			}
		});

		btnEdit.setImage(SWTResourceManager.getImage(BasicFormMaster.class,
				"/images/form/database_edit.png"));
		btnEdit.setText("Edit");

		btnDelete = new ToolItem(toolBar, SWT.NONE);
		btnDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent evt) {
				btnDeleteWidgetSelected(evt);
			}
		});
		btnDelete.setImage(SWTResourceManager.getImage(BasicFormMaster.class,
				"/images/form/database_delete.png"));
		btnDelete.setText("Delete");

		btnCancel = new ToolItem(toolBar, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent evt) {
				btnCancelWidgetSelected(evt);
			}
		});

		btnCancel.setImage(SWTResourceManager.getImage(BasicFormMaster.class,
				"/images/form/database_refresh.png"));
		btnCancel.setText("Cancel");

		grpData = new Composite(this, SWT.BORDER);
		grpData.setLayout(new FillLayout(SWT.HORIZONTAL));
		grpData.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1,
				1));
		getPanelContent(grpData);

		additionalProses();

		// JANGAN DIBALIK URUTANNYA DENGAN ADITIONAL PROSES
		// MENGAKIBATKAN COMBO TIDAK BERFUNGSI
		loadData();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	protected final void loadData() {
		// LOAD DATA SESUAI KEY
		try {
			if (getFormMode() == AYConst.MODE_VIEW) {
				doLoad(getFormKey());

				// DISABLED KONTROL
				enabledControl(false, false);
				disabledAddButton(true);
			} else {
				doLoadAdd(null);
				// set form mode to new
				enabledControl(true, true);
				disabledAddButton(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Util.showMsgBox("Gagal load: " + e.getMessage(), 1, getShell());
		}
	}

	protected final void disabledAddButton(boolean enabled) {
		btnSave.setEnabled(!enabled);
		btnEdit.setEnabled(enabled);
		btnDelete.setEnabled(enabled);
		btnCancel.setEnabled(false);
		disabledOtherButton(enabled);
	}

	protected final boolean saveData() {
		if (!doValidate()) {
			return false;
		}

		if (getFormMode() == AYConst.MODE_ADD) {
			if (!doInsert()) {
				return false;
			}
		} else if (getFormMode() == AYConst.MODE_EDIT) {
			if (!doUpdate()) {
				return false;
			}
		}
		return true;
	}

	protected abstract int doMainInsert() throws Exception;

	protected abstract int doMainUpdate() throws Exception;

	protected abstract int doMainDelete() throws Exception;

	protected abstract void setPojoWithData() throws Exception;

	protected abstract void getPanelContent(Composite grpData);

	protected abstract void additionalProses();

	protected abstract boolean doValidate();

	protected abstract void doLoad(String key) throws Exception;

	protected abstract void enabledControl(boolean otherfield, boolean key);

	protected abstract void clearContols();

	protected abstract void disabledOtherButton(boolean enable);

	protected abstract void doLoadAdd(String key);

	protected boolean doInsert() {
		boolean rtrValue = true;

		try {
			int n = doMainInsert();
			if (n < 1) {
				return false;
			}
			Util.OPERATION_CODE = 1;
		} catch (Exception e) {
			e.printStackTrace();

			if (e.getMessage() != null && e.getMessage().contains("Duplicate")) {
				Util.showMsgBox(
						AYConst.TRX_FAIL
								+ ", karena kode/primary key sudah ada, silakan gunakan yang lain!",
						2, getShell());
			} else {
				Util.showMsgBox(
						AYConst.TRX_FAIL + ", karena: " + e.getMessage(), 2,
						getShell());
			}
			rtrValue = false;
		}

		return rtrValue;
	}

	protected boolean doUpdate() {
		boolean rtrValue = true;

		try {
			int n = doMainUpdate();
			if (n < 1) {
				return false;
			}
			Util.OPERATION_CODE = 2;
		} catch (Exception e) {
			e.printStackTrace();
			Util.showMsgBox(AYConst.TRX_FAIL + ", karena: " + e.getMessage(),
					2, getShell());
			rtrValue = false;
		}

		return rtrValue;
	}

	protected boolean doDelete() {
		boolean rtrValue = true;

		MessageBox messageBox = new MessageBox(getShell(), SWT.OK | SWT.CANCEL
				| SWT.ICON_WARNING);
		messageBox.setMessage(AYConst.CONFIRM_DELETE);

		if (messageBox.open() == SWT.OK) {
			try {
				doMainDelete();
				Util.OPERATION_CODE = 3;
				Util.showMsgBox(AYConst.TRX_SUCCESS, 0, getShell());
				getShell().dispose();
			} catch (Exception e) {
				e.printStackTrace();
				rtrValue = false;
				Util.showMsgBox(
						AYConst.TRX_FAIL + ", karena: " + e.getMessage(), 2,
						getShell());
			}
		}

		return rtrValue;
	}

	private final void btnSaveWidgetSelected(SelectionEvent evt) {
		if (!saveData()) {
			return;
		}

		enabledControl(false, false);
		setFormMode(AYConst.MODE_VIEW);
		disabledAddButton(true);

	}

	private String funcId;

	public String getFuncId() {
		return funcId;
	}

	public void setFuncId(String funcId) {
		this.funcId = funcId;
	}

	private final void btnEditWidgetSelected(SelectionEvent evt) {
		if (!Util.isAllowedEdit(getFuncId())) {
			Util.showMsgBox("Anda tidak mempunyai hak untuk edit data", 1,
					getShell());
			return;
		}
		editData();
	}

	private final void btnDeleteWidgetSelected(SelectionEvent evt) {
		if (!Util.isAllowedDelete(getFuncId())) {
			Util.showMsgBox("Anda tidak mempunyai hak untuk hapus data", 1,
					getShell());
			return;
		}
		doDelete();
	}

	protected final String getFormKey() {
		return formKey;
	}

	public final void setFormKey(String key) {
		this.formKey = key;
	}

	protected final int getFormMode() {
		return formMode;
	}

	protected final void setFormMode(int frmMode) {
		this.formMode = frmMode;
	}

	protected final void editData() {
		disabledAddButton(false);
		enabledControl(true, false);
		btnCancel.setEnabled(true);
		setFormMode(AYConst.MODE_EDIT);
	}

	protected final void newData() {
		disabledAddButton(false);
		enabledControl(true, true);
		btnCancel.setEnabled(true);
		clearContols();
		setFormMode(AYConst.MODE_ADD);
	}

	public Composite getGrpData() {
		return grpData;
	}

	public void setGrpData(Composite grpData) {
		this.grpData = grpData;
	}

	private void btnCancelWidgetSelected(SelectionEvent evt) {
		try {
			doLoad(getFormKey());
		} catch (Exception e) {
			e.printStackTrace();
			Util.showMsgBox("Gagal load: " + e.getMessage(), 1, getShell());
		}
		enabledControl(false, false);
		disabledAddButton(true);
		setFormMode(AYConst.MODE_VIEW);
	}

	public void exportToExcel() {
		// TODO Auto-generated method stub

	}

}
